<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标的编码" prop="code">
        <el-select
          @change="(val) => changeOption(val, 'search')"
          v-model="queryParams.code"
          filterable
          remote
          placeholder="请输入标的编码"
          :remote-method="remoteMethod"
          :loading="loading">
          <el-option
            v-for="item in options"
            :key="item.code"
            :label="item.code"
            :value="item.code">
            <span>{{ item.code }} -- {{ item.codeName }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="标的名称" prop="codeName">
        <el-input
          :disabled="true"
          v-model="queryParams.codeName"
          placeholder="请输入标的编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['work:buyinfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="handleDelete"
          v-hasPermi="['work:buyinfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['work:buyinfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="buyInfoList">
      <el-table-column label="姓名" width="100" align="center" prop="nickName" />
      <el-table-column label="标的编码" width="100" align="center" prop="code" />
      <el-table-column label="标的名称" width="100" align="center" prop="codeName" />
      <el-table-column label="名义本金" width="100" align="center" prop="benjin" />
      <el-table-column label="费率" width="60" align="center" prop="rate" />
      <el-table-column label="期权费" width="90" align="center" prop="amount" />
      <el-table-column label="天数" width="60" align="center" prop="days" />
      <el-table-column label="起始日" width="120" align="center" prop="startDate" />
      <el-table-column label="到期日" width="120" align="center" prop="endDate" />
      <el-table-column label="成交价" width="60" align="center" prop="cj" />
      <el-table-column label="结构" width="60" align="center" prop="struct" />
      <el-table-column label="执行价" width="60" align="center" prop="zxj" />
      <el-table-column label="现价" width="60" align="center" prop="xj" />
      <el-table-column label="参考净收益" width="140" align="center" prop="sy" />
      <el-table-column label="对手方" width="100" align="center" prop="dsf" />
      <el-table-column label="备注" width="200" align="center" prop="mark" />
      <el-table-column label="操作" width="100" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.own"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['work:buyinfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['work:buyinfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改购买信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标的编码">
          <el-select
            @change="(val) => changeOption(val, 'dialog')"
            v-model="form.code"
            filterable
            remote
            placeholder="请输入标的编码"
            :remote-method="remoteMethod"
            :loading="loading">
            <el-option
              v-for="item in options"
              :key="item.code"
              :label="item.code"
              :value="item.code">
              <span>{{ item.code }} -- {{ item.codeName }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标的名称" prop="codeName">
          <el-input v-model="form.codeName" :disabled="true" placeholder="请输入标的编码" />
        </el-form-item>
        <el-form-item label="名义本金" prop="benjin">
          <el-input-number v-model="form.benjin" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="费率" prop="rate">
          <el-input-number v-model="form.rate" controls-position="right" :precision="2" :min="0" />
        </el-form-item>
        <el-form-item label="时间范围" :rules="rules">
          <el-date-picker
            v-model="dateRange"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="changeDate"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="成交价" prop="cj" :precision="2">
          <el-input-number v-model="form.cj" controls-position="right" :precision="2" :min="0" />
        </el-form-item>
        <el-form-item label="结构" prop="struct">
          <el-input-number v-model="form.struct" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="对手方" prop="dsf">
          <el-input
            v-model="form.dsf"
            placeholder="请输入对手方名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="备注" prop="mark">
          <el-input v-model="form.mark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBuyInfo, getBuyInfo, delBuyInfo, addBuyInfo, updateBuyInfo } from "@/api/work/buyinfo";
import { queryCompany } from "@/api/work/company";

export default {
  name: "index",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 价格表格数据
      buyInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: undefined,
        codeName: undefined
      },
      // 表单参数
      form: {},
      options: [],
      dateRange: [],
      // 表单校验
      rules: {
        codeName: [
          { required: true, message: "标的名称不能为空", trigger: "blur" }
        ],
        code: [
          { required: true, message: "标的代码不能为空", trigger: "blur" }
        ],
        dateRange: [
          { required: true, message: "日期不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // change code 方法
    changeOption(val, extra){
      this.options.map((item, index) => {
        if(item.code == val) {
          if(extra == 'dialog') {
            this.form.code = item.code
            this.form.codeName = item.codeName
          } else if(extra == 'search') {
            this.queryParams.code = item.code
            this.queryParams.codeName = item.codeName
          }
        }
      })
    },
    // change code 方法
    changeDate(e){
        if(e[0] != '') {
          this.form.startDate = e[0]
        }

        if(e[1] != '') {
          this.form.endDate = e[1]
        }

        if(e[0] != '' && e[1] != '') {
          this.form.days = (Date.parse(e[1]) - Date.parse(e[0])) / (1000 * 3600 * 24)
        }
    },
    remoteMethod(query) {
      if (query !== '') {
        this.loading = true;
        queryCompany(query).then(response => {
          this.loading = false;
          console.log(response)
          this.options = response.data
        })
      } else {
        this.options = [];
      }
    },
    /** 查询价格列表 */
    getList() {
      this.loading = true;
      listBuyInfo(this.queryParams).then(response => {
        this.buyInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.dateRange = []
      this.form = {
        id: undefined,
        code: undefined,
        codeName: undefined,
        benjin: undefined,
        rate: undefined,
        startDate: undefined,
        endDate: undefined,
        days: undefined,
        xj: undefined,
        struct: undefined,
        dsf: undefined,
        mark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id
      getBuyInfo(id).then(response => {
        this.form = response.data;
        this.dateRange.push(response.data.startDate)
        this.dateRange.push(response.data.endDate)
        this.open = true;
        this.title = "修改购买信息";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateBuyInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBuyInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const buyID = row.buyID;
      this.$modal.confirm('是否确认删除购买编号为"' + buyID + '"的数据项？').then(function() {
        return delBuyInfo(buyID);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('work/price/export', {
        ...this.queryParams
      }, `post_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped lang="scss">
.el-dialog {
  .el-select,
  .el-input-number,
  .el-date-editor{
    width: 100%!important;
  }
}

</style>
